home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3370 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: The size of a file
  5. Date: Sun, 28 Jan 1996 01:13:00 GMT
  6. Organization: Netcom
  7. Message-ID: <310acacf.92393344@nntp.ix.netcom.com>
  8. References: <4ebc03$4gv@gate.compart.fi> <4edo88$4c7@news.xs4all.nl>
  9. NNTP-Posting-Host: ix-dc6-02.ix.netcom.com
  10. X-NETCOM-Date: Sat Jan 27  5:12:53 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. falstaff@xs4all.nl (Falstaff) wrote:
  14.  
  15. > Fredrik Sandstrom <fred@spider.compart.fi> writes:
  16. > >This is driving me nuts.  I can't find a simple way to get the size of a
  17. > >disk file.  This must be possible using the standard C library, but I
  18. > >haven't figured out how!
  19. > f=fopen()
  20. > fseek(f,SEEK_END,0)
  21. > ftell(f)
  22. > fclose(f)
  23.  
  24. This may not work.
  25.  
  26. It's obvious that you intend this as an outline of the functions to
  27. call, not as an example of code and I am answering in that vein.
  28.  
  29. The problem is that if the file is opened in text mode (fopen("file",
  30. "r") ftell() is not required to required to return a byte offset --
  31. it's value is unspecified (i.e., the implementation need not document
  32. it).  It must, of course, be usable in fseek() to return to the
  33. current position.   (ISO 7.9.9.4)
  34.  
  35. This problem does not occur if the file is opened in binary mode
  36. (fopen("file", "rb")), but there we have the problem that the fseek()
  37. may not work as intended.  "A binary stream need not meaningfully
  38. support fseek calls with a whence value of SEEK_END" (ISO 7.9.9.2).
  39.  
  40. In practice this method does usually work, but if the implementation
  41. defines a (nonstandard) function for determining the size of the file
  42. I'd suggest considering it.  stat() is a common name for such a
  43. function.
  44.  
  45.  
  46. Michael M Rubenstein
  47.